package com.liqi.frame.comm.session;

import com.liqi.frame.comm.enums.OnlineStatusEnum;
import lombok.Data;
import org.apache.shiro.session.mgt.SimpleSession;

/**
 * 在线用户会话属性
 * @author liqi on 2020/1/19 0019. liqiwork@qq.com
 */
@Data
public class OnlineSession extends SimpleSession {
    private static final long serialVersionUID = 1L;

    /** 用户ID */
    private String userId;

    /** 用户名称 */
    private String loginName;

    /** 用户名称 */
    private String username;

    /** 部门名称 */
    private String groupName;

    /** 用户头像 */
    private String avatar;

    /** 登录IP地址 */
    private String host;

    /** 浏览器类型 */
    private String browser;

    /** 操作系统 */
    private String os;

    /** 在线状态 */
    private String status = OnlineStatusEnum.ONLINE.getKey();

    /** 属性是否改变 优化session数据同步 */
    private transient boolean attributeChanged = false;
    private OnlineStatusEnum statusEnum = OnlineStatusEnum.ONLINE;


    public void markAttributeChanged() {
        this.attributeChanged = true;
    }

    public void resetAttributeChanged() {
        this.attributeChanged = false;
    }

    public boolean isAttributeChanged() {
        return attributeChanged;
    }

    public OnlineStatusEnum getStatusEnum() {
        for (OnlineStatusEnum o: OnlineStatusEnum.values()) {
            if (o.getKey().equalsIgnoreCase(status)) {
                return o;
            }
        }
        return statusEnum;
    }

    public void setStatusEnum(OnlineStatusEnum statusEnum) {
        this.statusEnum = statusEnum;
        this.status = statusEnum.getKey();
    }



    @Override
    public void setAttribute(Object key, Object value) {
        super.setAttribute(key, value);
    }

    @Override
    public Object removeAttribute(Object key) {
        return super.removeAttribute(key);
    }
}
